package com.linzp.bishe.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.linzp.bishe.common.Result;
import com.linzp.bishe.entity.*;
import com.linzp.bishe.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 林泽鹏
 * @since 2021-11-01
 */
@RestController
@RequestMapping("/user")
public class UsersController {

	@Autowired
	private UsersService usersService;

	@Autowired
	private AccountsService accountsService;

	@Autowired
	private ParticipantsService participantsService;

	@Autowired
	private MeetingService meetingService;

	@Autowired
	private AchievementTransformationService achievementTransformationService;

	@Autowired
	private DisciplineCompetitionService disciplineCompetitionService;

	@Autowired
	private HprojectService hprojectService;

	@Autowired
	private PaperService paperService;

	@Autowired
	private PatentService patentService;

	@Autowired
	private PublicationsService publicationsService;

	@Autowired
	private ResearchAchievementsService researchAchievementsService;

	@Autowired
	private SoftwareCopyrightService softwareCopyrightService;

	@Autowired
	private TeachingRewardService teachingRewardService;

	@Autowired
	private ZprojectService zprojectService;

	/**
	 * @Description 通过token验证，若可以返回姓名
	 * @Param []
	 * @return 结果(com.linzp.bishe.common.Result)
	 */
	@GetMapping("/name")
	public Result getName(HttpServletRequest httpServletRequest){
		Accounts account = accountsService.checkAccountStatus(httpServletRequest);
		Result success = Result.success();
		QueryWrapper<Users> wrapper = new QueryWrapper<>();
		wrapper.eq("account_ID",account.getId());
		Users users = usersService.getOne(wrapper);
		success.put("name",users.getUserName());
		return success;
	}

	/**
	 * @Description 得到用户信息
	 * @Param [httpServletRequest]
	 * @return com.linzp.bishe.common.Result
	 */
	@GetMapping("")
	public Result getUser(HttpServletRequest httpServletRequest){
		Accounts account = accountsService.checkAccountStatus(httpServletRequest);
		Result success = Result.success();
		QueryWrapper<Users> wrapper = new QueryWrapper<>();
		wrapper.eq("account_ID",account.getId());
		Users users = usersService.getOne(wrapper);
		success.put("user",users);
		return success;
	}

	/**
	 * @Description 修改用户信息
	 * @Param [httpServletRequest]
	 * @return com.linzp.bishe.common.Result
	 */
	@PostMapping("")
	@Transactional
	public Result saveUser(HttpServletRequest httpServletRequest,@RequestBody Users user){
		Accounts account = accountsService.checkAccountStatus(httpServletRequest);
		user.setAccountId(account.getId());
		Result success = Result.success();
		QueryWrapper<Users> wrapper = new QueryWrapper<>();
		wrapper.eq("account_ID",account.getId());
		usersService.update(user, wrapper);
		QueryWrapper<Participants> wra = new QueryWrapper<>();
		wra.eq("users_ID",user.getId());
		Participants one = participantsService.getOne(wra,false);
		if(one != null & !one.getParticipantName().equals(user.getUserName())){
			participantsService.updateNameByUsersId(user.getId(),user.getUserName());
			usersService.updateName(achievementTransformationService,user.getId(),user.getUserName(),true);
			usersService.updateName(disciplineCompetitionService,user.getId(),user.getUserName(),true);
			usersService.updateName(hprojectService,user.getId(),user.getUserName(),true);
			usersService.updateName(paperService,user.getId(),user.getUserName(),true);
			usersService.updateName(patentService,user.getId(),user.getUserName(),true);
			usersService.updateName(publicationsService,user.getId(),user.getUserName(),true);
			usersService.updateName(researchAchievementsService,user.getId(),user.getUserName(),true);
			usersService.updateName(softwareCopyrightService,user.getId(),user.getUserName(),true);
			usersService.updateName(teachingRewardService,user.getId(),user.getUserName(),true);
			usersService.updateName(zprojectService,user.getId(),user.getUserName(),true);
			usersService.updateName(meetingService,user.getId(),user.getUserName(),false);
		}
		QueryWrapper<Meeting> wraw = new QueryWrapper<>();
		wraw.eq("user_ID",user.getId());
		Meeting meeting = meetingService.getOne(wraw, false);
		if (meeting != null & !meeting.getAttendee().equals(user.getUserName())){
			UpdateWrapper<Meeting> wm = new UpdateWrapper<>();
			wm.eq("user_ID",user.getId());
			wm.set("attendee",user.getUserName());
			meetingService.update(wm);
		}
		return success;
	}

	/**
	 * @Description 管理员更新密码
	 * @Param [user]
	 * @return com.linzp.bishe.common.Result
	 */
	@PostMapping("/admin")
	public Result updateUser(@RequestBody Users user){
		Result success = Result.success();
		usersService.updateAdminUser(user);
		return success;
	}




}
